#define _CRT_SECURE_NO_WARNINGS 1

#include <vector>
#include <iostream>
using namespace std;

class Solution {
public:
    int trap(vector<int>& height) {
        int ans = 0;
        int left = 0, right = height.size() - 1;
        int leftMax = 0, rightMax = 0;
        while (left < right) {
            leftMax = max(leftMax, height[left]);
            rightMax = max(rightMax, height[right]);
            if (height[left] < height[right]) {
                ans += leftMax - height[left];
                ++left;
            }
            else {
                ans += rightMax - height[right];
                --right;
            }
        }
        return ans;
    }
};




struct TestCase {
    vector<int> height;
    int expected;
};

int main() {
    vector<int> v1 = { 0,1,0,2,1,0,1,3,2,1,2,1 };
    vector<int> v2 = { 4,2,0,3,2,5 };
    Solution sol;
    cout << sol.trap(v1) << endl;
    cout << sol.trap(v2) << endl;
    return 0;
}